package interlogic.xml;

import java.util.ArrayList;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Bitmap.Config;
import android.util.Log;
import android.widget.ImageView;

public class InterLogicGraph
{
	private double Min;
	private double Max;
	
	public InterLogicGraph()
	{
		Min = 999999;
		Max = 0;
	}

    public void Paint(ImageView Graph, ArrayList<Double> Values)
    {
        int width = Graph.getWidth();
        int height = Graph.getHeight();
        Bitmap bitmap = Bitmap.createBitmap(width, height, Config.RGB_565);
        Paint paint = new Paint();
        paint.setColor(Color.BLUE);
        Canvas Canva = new Canvas(bitmap);
        Canva.drawRect(0, 0, width, height, paint);
        paint.setColor(Color.WHITE);
    	
        if (Values.size() > 0)
        {
	        Min = Values.get(0);
	        Max = Values.get(0);
	        for (int i = 1; i < Values.size(); i++)
	        {
	        	if (Values.get(i) < Min)
	        		Min = Values.get(i);
	        	if (Values.get(i) > Max)
	        		Max = Values.get(i);
	        }
	        int Vidstup = 50;
	        double KoefH = (double)Graph.getHeight() / ((Max - Min)*2);
	        double KoefW = ((double)Graph.getWidth() - 2*Vidstup) / (Values.size() - 1);
	        
	        String Text;
	        Text = String.valueOf(Max); Canva.drawText(Text, (Graph.getWidth() - Vidstup), (Graph.getHeight()/4), paint);
	        Text = String.valueOf(Min); Canva.drawText(Text, (Graph.getWidth() - Vidstup), (Graph.getHeight()*3/4), paint);
	        Text = String.valueOf(Max); Canva.drawText(Text, 0, (Graph.getHeight()/4), paint);
	        Text = String.valueOf(Min); Canva.drawText(Text, 0, (Graph.getHeight()*3/4), paint);
	        Canva.drawLine(Vidstup, Graph.getHeight()/4, Graph.getWidth() - Vidstup, Graph.getHeight()/4, paint);
	        Canva.drawLine(Vidstup, Graph.getHeight()*3/4, Graph.getWidth() - Vidstup, Graph.getHeight()*3/4, paint);
	        
	        //Log.w("Values size", String.valueOf(Values.size()));
	        for (int i = 0; i < Values.size() - 1; i++)
		    {
	        	Double randomInt1 = Graph.getHeight()/4 + (Max - Values.get(i))*KoefH;
	        	Double randomInt2 = Graph.getHeight()/4 + (Max - Values.get(i + 1))*KoefH;
	        	Canva.drawLine((int)(Vidstup + i*KoefW), randomInt1.intValue(), (int)(Vidstup + (i + 1)*KoefW), randomInt2.intValue(), paint);
		    	randomInt1 = randomInt2;
		    }
        }
        else
        {
        	Canva.drawText("Not enough data at this file", (Graph.getWidth()/3), (Graph.getHeight()/4), paint);
        }
        Graph.setImageBitmap(bitmap);
    }
    
}
